var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var config = require('../../config');
var util = require('../../utils/util.js')
//获取应用实例
var app = getApp()
var message = '';

var text = '';

var user = {};
var names = [];
var mgs = [];
Page({
  data: {
    buyChip: config.service.buyChip,
    getRoomInfo: config.service.getRoomInfo,
    getInfo: config.service.getInfo,
    inputValue: '0',
    name:'',
    items: [],
    message: '',
    text: text,
    msgs: [],
    room_id: 0,
    buy_chip: 0,
    chip: 0,
    room_name: '',
    count: 0,
    value: 0,
    avatarUrl: '',
    toView: 'bot',
    isLoading: false,
    users: []
  },
  add: function() {
    var self = this;
    self.setData({
      isLoading: true
    });
    qcloud.request({
      url: self.data.buyChip,
      data: {
         room_id: self.data.room_id,
         buy_chip_count: self.data.buy_chip_count
      },
      login: true,
      success(resp) {
          self.setData({
            isLoading: false
          });
          if(resp.data.code === 0){
              console.log(self.data.users);
              var date = util.formatTime(new Date());
              mgs.push({msg: resp.data.data, date: date});
              self.setData({
                msgs: mgs,
                value: parseInt(resp.data.data) + parseInt(self.data.value),
                toView: 'bot'
              });
          }else{
              util.showSuccess(resp.data.message);
          }
      },
      fail(error) {
          console.log('request fail', error);
      }
    });
  },
  leave: function() {
    var self = this;
    wx.redirectTo({
     url: '../mine/mine?room_id='+ self.data.room_id +'&room_name='+ self.data.room_name
    });
  },
  onLoad: function (options) {
    console.log(options)
    var self = this;
    self.setData({
      room_id: options.room_id,
      room_name: options.room_name,
      chip: options.chip,
      buy_chip_count: options.buy_chip_count
    });
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      user = userInfo;
      self.data.items.push({
          avatarUrl: userInfo.avatarUrl,
          value: options.buy_chip
      });
      self.data.name = userInfo.nickName;
      self.setData({
        items: self.data.items,
        avatarUrl: userInfo.avatarUrl,
        userInfo: userInfo,
        value: options.buy_chip,
        buy_chip: options.buy_chip
      });
    });
    wx.setNavigationBarTitle({
      title: self.data.room_name + ' 的房间'
    });
  },
  onShow: function(){
    var self = this;
    qcloud.request({
      url: self.data.getRoomInfo,
      data: {
         room_id: self.data.room_id
      },
      login: true,
      success(resp) {
          console.log(resp)
          if(resp.data.code === -6){
            wx.redirectTo({
              url: '../share/share?room_id='+ self.data.room_id +'&room_name=' + self.data.room_name +'&chip=' + self.data.chip
            });
          };
          if(resp.data.code === 0){
            if(resp.data.data.status == 1){
              qcloud.request({
                url: self.data.getInfo,
                data: {
                   room_id: self.data.room_id
                },
                login: true,
                success(resp) {
                    if(resp.data.code === 0){
                      var obj = resp.data.data.userlist;
                      var arr = [];
                      for(var key in obj){
                        obj[key].user_info = JSON.parse(obj[key].user_info);
                        arr.push(obj[key]);
                      };
                      self.setData({
                        users: arr
                      });
                      console.log(self.data.users);
                    }else{
                        util.showSuccess(resp.data.message);
                    }
                },
                fail(error) {
                    console.log('request fail', error);
                }
              });
            };
            if(resp.data.data.status == 2){
              if(resp.data.data.room_status == 1){
                wx.redirectTo({
                  url: '../mine/mine?room_id='+ self.data.room_id +'&room_name='+ self.data.room_name
                })
              }else{
                wx.redirectTo({
                  url: '../total/total?room_id='+ self.data.room_id +'&room_name='+ self.data.room_name
                })
              }
            };
          };
      },
      fail(error) {
          console.log('request fail');
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: '德扑房已开好，速来领筹码',
      path: 'pages/details/details?room_id=' + this.data.room_id +'&room_name='+ this.data.room_name +'&chip='+ this.data.chip+'&buy_chip=' + this.data.buy_chip+ '&buy_chip_count=' + this.data.buy_chip_count,
      success: function(res) {
        wx.showToast({
            title: '分享成功',
            icon: 'success'
        });
      },
      fail: function(res) {
        wx.showToast({
            title: '分享失败',
            icon: 'success'
        });
      }
    }
  }
})
